package com.aynu.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aynu.common.valid.AddGroup;
import com.aynu.common.valid.UpdateGroup;
import com.aynu.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.aynu.gulimall.product.entity.BrandEntity;
import com.aynu.gulimall.product.service.BrandService;
import com.aynu.common.utils.PageUtils;
import com.aynu.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author majun
 * @email mjun1833@foxmail.com
 * @date 2020-06-06 21:39:56
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @PostMapping("/infos")
    public R brandInfo(@RequestBody List<Long> brandIds){
        List<BrandEntity> brandEntities =  brandService.queryBrandByIds(brandIds);

        return R.ok().put("brand", brandEntities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * @Valid开启校验 和bean注解，结合使用
     * BindingResult result ,封装了前面的校验结果
     * 保存
     *@Valid 是Java提供的校验注解，没有任何属性
     *@Validated  是spring提供的校验注解，可以指定一个或多个校验分组
     *
     */
    @RequestMapping("/save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/){
        /*if (result.hasErrors()){
            Map<String,String> map = new HashMap<>();
            //1。获取校验结果
            result.getFieldErrors().forEach((item)->{
                //fileError获取到错误的提示
                String message = item.getDefaultMessage();
                //获取错误的属性名字
                String field = item.getField();
                map.put(field,message);
            });
            return R.error(400,"提交的数据不合法！").put("data",map);
        }else{
        }*/
        brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/status")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
